package com.cfh.springcloudprovider.controller;

import com.cfh.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author chenfh
 * @date 2019/7/2
 */

@RestController
public class HelloController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/hello")
    public String hello() throws InterruptedException {
        int sleepTime = new Random().nextInt(3000);
        System.out.println("sleepTime:" + sleepTime);
//        Thread.sleep(sleepTime);
//
//        List<String> services = discoveryClient.getServices();
//        for (String service : services) {
//            List<ServiceInstance> instances = discoveryClient.getInstances(service);
//            for (ServiceInstance instance : instances) {
//                System.out.println("/hello,host:" + instance.getHost() + ",service_id:" + instance.getServiceId());
//            }
//        }
        return "Hello World";
    }

    @GetMapping("/hello1")
    public String hello(@RequestParam String name) {
        return "Hello :" + name;
    }

    @GetMapping("/hello2")
    public User hello(@RequestHeader String name, @RequestHeader Integer age) {
        return new User(name, age);
    }

    @PostMapping("/hello3")
    public String hello(@RequestBody User user) {
        return "Hello :" + user.getName() + "," + user.getAge();
    }


}
